<?php

App::uses('AppModel', 'Model');

/**
 * Product Model
 *
 * @property Image $Image
 */
class Product extends AppModel {

    /**
     * Display field
     *
     * @var string
     */
    public $displayField = 'title';

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'title' => array(
            array(
                'rule' => 'notEmpty',
                'message' => 'Ingrese un Título',
                //'allowEmpty' => false,
                'required' => true,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'lead' => array(
            array(
                'rule' => 'notEmpty',
                'message' => 'Your custom message here',
            ),
        ),
        'body' => array(
            array(
                'rule' => 'notEmpty',
                'message' => 'Your custom message here',
            ),
        ),       
    );
    
    public $actsAs = array(
        'Uploader.Attachment' => array(
            'technical' => array(
                'name' => 'formatFileName', // Name of the function to use to format filenames
                'uploadDir' => 'uploads/products/technical/', // See UploaderComponent::$uploadDir
                'dbColumn' => 'technical', // The database column name to save the path to
                'maxNameLength' => 40,
                'overwrite' => true, // Overwrite file with same name if it exists
                'stopSave' => true, // Stop the model save() if upload fails
                'allowEmpty' => true, // Allow an empty file upload to contin
            )
        ),
        'Uploader.FileValidation' => array(
            'fileName' => array(
                'extension' => array(
                    'value' => array('pdf'),
                ),
            )
        )        
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * hasMany associations
     *
     * @var array
     */
    public $hasMany = array(
        'Image' => array(
            'className' => 'Image',
            'foreignKey' => 'product_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    function formatFileName($name, $field, $file) {
        return md5($name);
    }

    public function beforeSave() {
        if (!empty($this->data)) {
            // do something
            //die(var_dump($this->data));
        }
        return true;
    }

}
